VERSION					= 2
PATCHLEVEL				= 0
SUBLEVEL				= 0
EXTRAVERSION			=

$(if $(filter __%, $(MAKECMDGOALS)), \
	$(error targets prefixed with '__' are only for internal use))

EEL_DATETIME			= $(shell date +'%Y%m%d_%H%M%S')
EEL_GIT_FILES			= $(shell git ls-files --with-tree=HEAD --full-name)
EEL_GIT_COMMIT			= $(shell git rev-list --max-parents=0 HEAD | cut -c 1-8)
EEL_GIT_DIRTY			=
ifneq ($(shell git status --null),)
EEL_GIT_DIRTY			= -dirty
endif

PWD := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
# EELDIR := $(realpath $(shell cd $(PWD); git rev-parse --show-toplevel))
EELDIR := $(PWD)
export EELDIR

CONF					?= .tmp.config.mk
-include $(CONF)

# CONF file may define ARCH
# NOTICE: if use make -C, CONF must be abspath
# ifneq ($(filter help%,$(MAKECMDGOALS)),$(MAKECMDGOALS))
ifeq ($(ARCH),)
$(info !!! Please define ARCH, such as './select' or CONF=xxx !!!)
$(error exiting)
endif

MAKE_THIS				:= $(MAKE) --no-print-directory -f Makefile

# Disable the built-in implicit rules and built-in variable settings.
# add -rR would cause buildroot fail, so unexport
MAKEFLAGS				+= -rR
unexport MAKEFLAGS

#### Default Target, before including *.make ####
__PHONY := __all
__all: info help
	@:	# do nothing

include function.include

#### config mk start ####
# HOST_OS: debian/ubuntu
HOST_OS					?= $(shell grep '^ID=.*' /etc/os-release | sed -e 's%ID=%%')
include $(wildcard os/$(HOST_OS).mk)

include $(wildcard arch/$(ARCH)/*.mk)

# arm or arm64
ifneq ($(findstring arm,$(ARCH)),)
include $(wildcard configs/arm/*.mk)
endif

# riscv32 or riscv64
ifneq ($(findstring riscv,$(ARCH)),)
include $(wildcard configs/riscv/*.mk)
endif

# ARCH config may need ovelay configs
include $(wildcard configs/*.mk)
#### config mk end ####

#### submake targets start ####
# for make -n, do not use $(MAKE) -f
include $(wildcard submake/*.make)

# arm or arm64
ifneq ($(findstring arm,$(ARCH)),)
include $(wildcard submake/arm/*.make)
endif

# riscv32 or riscv64
ifneq ($(findstring riscv,$(ARCH)),)
include $(wildcard submake/riscv/*.make)
endif
#### submake targets end ####

# help targets
$(foreach mod,$(sort $(EEL_MODS)),$(eval PHONY += $(PHONY-$(mod))))
$(foreach mod,$(sort $(EEL_MODS)),$(eval HELPS += help-$(mod)))

PHONY += info help $(HELPS)
info:
	@if [ -f .tmp.config.mk ]; then \
		echo "current: .tmp.config.mk -> " $(realpath .tmp.config.mk); \
	else \
		echo ".tmp.config.mk not found"; \
	fi

help:
	@echo "help targets for $(ARCH):"
	@$(call echo-list,\t,help info)
	@$(call echo-list,\t,$(HELPS))

# it is does not metter if HELP_CMD-xxx was not defined
$(HELPS):
	@$(HELP_CMD-$(subst help-,,$@))
	@echo "$(subst help-,,$@) targets for $(ARCH):"
	@$(call echo-list,\t,$(PHONY-$(subst help-,,$@)))

.PHONY: $(__PHONY) $(PHONY)
